package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.model.LonLat;
import com.astrob.nhcmap.R;
import com.astrob.util.CoordChange;
import com.astrob.util.SysUtils;
import com.astrob.view.BaseViewHolder;
import com.nhc.model.HCLocationInfo;
import com.nhc.model.TransParaSeven;
import com.nhc.model.TransParaSevenFileHandle;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.PageScroller;

public class LocationActivity extends BaseActivity {
    private final String TAG = "LocationActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, LocationActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_location;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_location = (ListView) findViewById(R.id.lv_location);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_location.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<Integer, String>> locationlist;
    private ArrayList<HCLocationInfo> mHclList = new ArrayList<HCLocationInfo>();

    private void resetHCLocationInfo() {
        mHclList.clear();

        int coord_format = SysUtils.getInstance().coord_format;
        LonLat ll = SysUtils.getInstance().getLL();
//        ll = new LonLat(116.327780, 39.986670);
        for (int i = 0; i < SysUtils.COORD_0.length; i++) {
            if (coord_format <= 2) {
                if (i == 0) {
                    mHclList.add(CoordChange.getInstance().getHCL(i, ll, coord_format));
                } else {
                    mHclList.add(CoordChange.getInstance().getHCL(i, ll, 4));
                }
            } else {
                if (i == 0) {
                    mHclList.add(CoordChange.getInstance().getHCL(i, ll, 2));
                } else {
                    mHclList.add(CoordChange.getInstance().getHCL(i, ll, coord_format));
                }
            }

            //test  1-BJ54  2-XA80
//            HCLocationInfo hcl = mHclList.get(i);
//            Map<String, String> DMS = hcl.getLLStr();
//            String strLL = DMS.get("lat") + "\n" + DMS.get("lon");
//
//            int coord_mode = i;
//            int coord_format = SysUtils.getInstance().coord_format;
//            LonLat llA = CoordChange.getInstance().change2LL(strLL, coord_mode, coord_format);
//
//            coord_mode = 2;
        }


        //
//        ll = new LonLat(116.32778123, 39.98667123);//39.986672,116.327775
//        HCLocationInfo hcl = CoordChange.getInstance().getHCL(SysUtils.getInstance().coord_mode, ll);
//
//        int coord_mode = SysUtils.getInstance().coord_mode;
//        int coord_format = SysUtils.getInstance().coord_format;
//        Map<String, String> DMS = hcl.getLLStr();
//        String strLL = hcl.getLL().GetLat() + "\n" + hcl.getLL().GetLon();
//        LonLat llA = CoordChange.getInstance().getLL(hcl.getLL().GetLon(), hcl.getLL().GetLat(), 0, "", coord_mode, coord_format);
//
//        int a = 0;
//        a++;


    }

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                resetHCLocationInfo();
                locationlist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(locationlist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    protected List<Entry<Integer, String>> getList() {
        locationlist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mHclList.size(); i++) {
            locationlist.add(new Entry<Integer, String>(i, ""));
        }
        return locationlist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_location.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onClickItem(position);

            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_location).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    private void onClickItem(int position) {
        if (position == 0) {
            //WGS 1984 禁止设置
            return;
        }

//        toActivity(CoordParmActivity.createIntent(this, position), 0);
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法
    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应

    }

    @Override
    public void finish() {
        TransParaSevenFileHandle.getInstance().save();
        super.finish();
    }

    private final int REQUEST_TO_CP = 1;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        if (requestCode == REQUEST_TO_CP) {
            initData();
            return;
        }
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.hclocation_view, parent, false);
            }

            TextView tv_citytitle = BaseViewHolder.get(convertView, R.id.tv_citytitle);
            TextView tv_lat = BaseViewHolder.get(convertView, R.id.tv_lat);
            TextView tv_lon = BaseViewHolder.get(convertView, R.id.tv_lon);

            tv_lat.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
            tv_lon.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));


            final Entry<Integer, String> kvb = getItem(position);
            int index = kvb.getKey();
            if (index >= 0 && index < mHclList.size()) {
                HCLocationInfo hcl = mHclList.get(index);
                tv_citytitle.setText(hcl.getCity());
                if (position == 0) {
                    Map<String, String> DMS = hcl.getLLDMS();
                    tv_lat.setText(DMS.get("lat"));
                    tv_lon.setText(DMS.get("lon"));
                } else {
                    Map<String, String> DMS = hcl.getLLStr();
                    tv_lat.setText(DMS.get("lat"));
                    tv_lon.setText(DMS.get("lon"));
                }

                TransParaSeven transParaSeven = TransParaSevenFileHandle.getInstance().getListTransParaSeven(index);
                if (!transParaSeven.isChange()) {
                    tv_lat.setTextColor(context.getResources().getColor(R.color.gray));
                    tv_lon.setTextColor(context.getResources().getColor(R.color.gray));
                }

            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}
